package customerEnter.Frame.scanning.scanningServer;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.dom4j.DocumentException;

import customerEnter.Frame.Annotation.FrameAttributes;
import customerEnter.Frame.Annotation.FrameExtraAttributes;
import customerEnter.Frame.DaoDataTransmission.ConfigXml;
import customerEnter.Frame.DaoDataTransmission.ConfigXmlBean;
import customerEnter.Frame.DaoDataTransmission.WebAppsInfo;
import customerEnter.Frame.Utils.FileUtils;
import customerEnter.Frame.Utils.Log4j;
import customerEnter.Frame.bean.FrameBeansModels;
import customerEnter.Frame.scanning.ScanningAnalysis;
import customerEnter.Frame.scanning.ScanningExtendedConfig;
import customerEnter.tool.WhetherToEmpty;

/**
 * 掃描包的類
 * 
 * 类名称：ProjectScanningServer 类描述： 创建人：dy 创建时间：2017-4-5 下午5:52:42
 * 
 */
public class ProjectScanningServer implements ScanningExtendedConfig {

	@Override
	public Map<String, Class<?>> scanningPackage(ScanningAnalysis analysis) {
		Map<String, Class<?>> map = new HashMap<String, Class<?>>();
		ConfigXml configXml = null;
		try {
			configXml = analysis.scanningConfigPackage(WebAppsInfo.webUrl + "\\WEB-INF\\classes\\frame.xml");
		} catch (DocumentException e) {
			Log4j.Log4jOutInfo(e.getMessage());
		}
		if (WhetherToEmpty.ObjectWhetherToEmpty(configXml)) {
			return new HashMap<String, Class<?>>();
		}
		/**
		 * 循環第一層標籤
		 */
		for (ConfigXmlBean configXmlBean : configXml.getConfigXmlBeans()) {
			/**
			 * 循環第二層標籤
			 */
			for (ConfigXmlBean configXmlBean2 : configXmlBean.getLabelBean()) {
				try {

					String path = WebAppsInfo.webUrl + "/WEB-INF/classes/"
							+ configXmlBean2.getValue().replace(".", "/");
					System.out.println("掃描路徑：" + path);
					/**
					 * 循環掃描包下面的文件
					 */
					FileUtils.readfile(path, map, "");
				} catch (Exception e) {
					Log4j.Log4jOutInfo(e.getMessage());
				}
			}
		}
		return map;
	}

	public Map<String, Field[]> scanningClass(Map<String, Class<?>> map) {
		Map<String, Field[]> fiMap = new HashMap<String, Field[]>();
		for (Entry<String, Class<?>> entry : map.entrySet()) {
			Field[] fields = entry.getValue().getDeclaredFields();
			if (IfFrameAttributes(fields)) {
				Map<String, Field> fMap = new HashMap<String, Field>();
				getClassFields(fields, fMap);
				Field[] fields2 = new Field[] {};
				int i = 0;
				for (Entry<String, Field> entry2 : fMap.entrySet()) {
					fields2[i] = entry2.getValue();
					i++;
				}
			} else {
				fiMap.put(entry.getKey(), fields);
			}
		}
		return fiMap;
	}

	public boolean IfFrameAttributes(Field[] fields) {
		boolean bool = false;
		for (Field field : fields) {
			field.setAccessible(true);
			if (!WhetherToEmpty.ObjectWhetherToEmpty(field.getAnnotation(FrameExtraAttributes.class))) {
				bool = true;
				break;
			}
		}
		return bool;
	}

	public void getClassFields(Field[] fields, Map<String, Field> map) {
		for (Field field : fields) {
			field.setAccessible(true);
			Annotation annotation = field.getAnnotation(FrameExtraAttributes.class);
			if (!WhetherToEmpty.ObjectWhetherToEmpty(annotation)) {
				continue;
			}
			map.put(field.getName(), field);
			annotation = field.getAnnotation(FrameAttributes.class);
			if (!WhetherToEmpty.ObjectWhetherToEmpty(annotation)) {
				FrameAttributes frameAttributes = (FrameAttributes) annotation;
				Class<?> class1 = frameAttributes.ClassName();
				getClassFields(class1.getDeclaredFields(), map);
			}
		}
	}

	public Map<String, FrameBeansModels> scanningClassModels(Map<String, Class<?>> map) {
		return null;

	}

	@Override
	public void getFrameBeansModels(Field[] fieldsOld, FrameBeansModels frameBeansModel) {
		// TODO Auto-generated method stub

	}
}
